【尚筹网】IDEA版实现(二)管理员登录 |
您所在的位置:网站首页 › idea test测试 › 【尚筹网】IDEA版实现(二)管理员登录 |
由于尚硅谷的视频是通过Eclipse软件来做的,其中有些操作与IDEA上有所区别,所以我在这里将IDEA区别于Eclipse的操作、操作过程中涉及的源码(与视频的源码略有出入)以及大家可能遇到的种种问题分享给大家,这些源码在我这里均是通过测试的,仅供参考! 1 MD5加密工具 1.1 MD5方法在util\src\main\java\com\atguigu\crowd\util\CrowdUtil.java中增加md5加密方法: /* * 对明文字符串进行MD5加密 * @param source 传入的明文字符串 * @return 加密结果 */ public static String md5(String source) { // 1.判断source是否有效 if (source == null || source.length() == 0) { // 2.如果不是有效的字符串则抛异常 throw new RuntimeException(CrowdConstant.MESSAGE_STRING_INVALIDATE); } try { // 3.获取MessageDigest对象 String algorithm = "md5"; MessageDigest messageDigest = MessageDigest.getInstance(algorithm); // 4.获取明文字符串对应的字节数组 byte[] input = source.getBytes(); // 5.执行加密 byte[] output = messageDigest.digest(input); // 6.创建BigInteger对象 int signum = 1; BigInteger bigInteger = new BigInteger(signum, output); // 7.按照16进制将bigInteger的值转换为字符串 int radix = 16; String encoded = bigInteger.toString(radix).toUpperCase(); return encoded; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return null; }在util\src\main\java\com\atguigu\crowd\constant\CrowdConstant.java中增加新字符常量: public static final String MESSAGE_STRING_INVALIDATE = "数据不合法,请不要传入空字符串!"; 1.2 测试类在webui\src\test\java\com\atguigu\crowd\test新建测试类StringTest.java public class StringTest { @Test public void testMD5(){ String source = "123123"; String encoded = CrowdUtil.md5(source); System.out.println(encoded); } }测试结果如下: 在util\src\main\java\com\atguigu\crowd\exception新建LoginFailedException.java public class LoginFailedException extends RuntimeException{ private static final long serialVersionUID = 1L; public LoginFailedException() { super(); } public LoginFailedException(String message) { super(message); } public LoginFailedException(String message, Throwable cause) { super(message, cause); } public LoginFailedException(Throwable cause) { super(cause); } public LoginFailedException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } }在component\src\main\java\com\atguigu\crowd\mvc\config\CrowdExceptionResolver.java中新增登录失败的异常处理方法 @ExceptionHandler(value = LoginFailedException.class) public ModelAndView resolveLoginFailedException(NullPointerException exception, HttpServletRequest request, HttpServletResponse response) throws IOException { String viewName = "admin-login"; return commonResolveException(viewName,exception,request,response); }在webui\src\main\webapp\WEB-INF\admin-login.jsp增加异常消息显示(在管理员登录标题下一行添加) 管理员登录${requestScope.exception.message} 2.2 Handler方法在component\src\main\java\com\atguigu\crowd\mvc\handler新建AdminHandler.java package com.atguigu.crowd.mvc.handler; import com.atguigu.crowd.constant.CrowdConstant; import com.atguigu.crowd.entity.Admin; import com.atguigu.crowd.service.api.AdminService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import javax.servlet.http.HttpSession; @Controller public class AdminHandler { @Autowired private AdminService adminService; @RequestMapping(value = "/admin/do/login.html") public String doLogin( @RequestParam("loginAcct") String loginAcct, @RequestParam("userPswd") String userPswd, HttpSession session ) { // 调用Service方法执行登录检查 // 这个方法如果能够返回admin对象说明登录成功,不正确则会抛出异常 Admin admin = adminService.getAdminByLoginAcct(loginAcct, userPswd); // 将登录成功返回的admin对象存入Session域 session.setAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN, admin); return "admin-main"; } }注意:@RequestParam注解获取的标签在admin-login.jsp中给出并对应(loginAcct与userPswd),我发现在之前的编写出漏掉了标签构建,这边重新添加一下,如果发现传参失败即为标签未添加: 并在webui\src\main\webapp\WEB-INF初始化一个admin-main.jsp 在util\src\main\java\com\atguigu\crowd\constant\CrowdConstant.java中增加新字符常量: public static final String ATTR_NAME_LOGIN_ADMIN = "loginAdmin"; 2.3 Service方法首先通过快捷键创建adminService.getAdminByLoginAcct(loginAcct, userPswd);的方法及实现类(快速新建方法Alt+Shift+Enter、快速添加实现类Ctrl+I) @Override public Admin getAdminByLoginAcct(String loginAcct, String userPswd) { // 1. 根据登录账号查询Admin对象 // 创建AdminExample对象 AdminExample adminExample = new AdminExample(); // 创建Criteria对象 AdminExample.Criteria criteria = adminExample.createCriteria(); // 在Criteria对象中封装查询条件 criteria.andLoginAcctEqualTo(loginAcct); // 这里跟视频中不太一样,因为方法名不同 // 调用AdminMapper的方法执行查询 List list = adminMapper.selectByExample(adminExample); // 2.判断Admin对象是否为null if (list == null || list.size() == 0) { throw new LoginFailedException(CrowdConstant.MESSAGE_LOGIN_FAILED); } if (list.size()>1) { throw new RuntimeException(CrowdConstant.MESSAGE_SYSTEM_ERROR_LOGIN_NOT_UNIQUE); } Admin admin = list.get(0); // 3.如果Admin对象为null则抛出异常 if (admin == null) { throw new LoginFailedException(CrowdConstant.MESSAGE_LOGIN_FAILED); } // 4.Admin对象不为null,取出密码 String userPswdDB = admin.getUserPswd(); // 5.将表单提交的明文密码进行加密 String userPswmForm = CrowdUtil.md5(userPswd); // 6.比较密码 if (!Objects.equals(userPswdDB, userPswmForm)) { // 7.如果比较结果不一致则抛出异常 throw new LoginFailedException(CrowdConstant.MESSAGE_LOGIN_FAILED); } // 8.如果一致则返回Admin对象 return admin; }在util\src\main\java\com\atguigu\crowd\constant\CrowdConstant.java中增加新字符常量: public static final String MESSAGE_SYSTEM_ERROR_LOGIN_NOT_UNIQUE = "系统错误:登录账号不唯一!"; 2.4 测试类将网页源代码复制到webui\src\main\webapp\WEB-INF\admin-main.jsp中 .tree li { list-style-type: none; cursor:pointer; } .tree-closed { height : 40px; } .tree-expanded { height : auto; } $(function () { $(".list-group-item").click(function(){ if ( $(this).find("ul") ) { $(this).toggleClass("tree-closed"); if ( $(this).hasClass("tree-closed") ) { $("ul", this).hide("fast"); } else { $("ul", this).show("fast"); } } }); }); 众筹平台 - 控制面板 ${sessionScope.loginAdmin.userName} 个人设置 消息 退出系统 帮助 控制面板 权限管理 3 用户维护 角色维护 菜单维护 业务审核 3 实名认证审核 广告审核 项目审核 业务管理 7 资质维护 分类管理 流程管理 广告管理 消息模板 项目分类 项目标签 参数管理 控制面板打开Tomcat并进入http://localhost:8080/atcrowdfunding02_admin_webui_war_exploded/admin/to/login/page.html,输入账号密码(账号密码在SQL中可查,建议保存一个): 如果输入错误,则弹出: 输入正确进入管理员页面: 修改component\src\main\java\com\atguigu\crowd\mvc\handler\AdminHandler.java return "redirect:/admin/to/main.html";修改webui\src\main\resources\spring-web-mvc.xml 4 退出登录修改component\src\main\java\com\atguigu\crowd\mvc\handler\AdminHandler.java @RequestMapping(value = "/admin/do/logout.html") public String doLogout(HttpSession session) { // 强制Session失效 session.invalidate(); return "redirect:/admin/to/login/page.html"; }在点击退出系统后可以返回管理员登录界面: 在webui\src\main\webapp\WEB-INF新建include-head.jsp .tree li { list-style-type: none; cursor:pointer; } .tree-closed { height : 40px; } .tree-expanded { height : auto; } $(function () { $(".list-group-item").click(function(){ if ( $(this).find("ul") ) { $(this).toggleClass("tree-closed"); if ( $(this).hasClass("tree-closed") ) { $("ul", this).hide("fast"); } else { $("ul", this).show("fast"); } } }); });在webui\src\main\webapp\WEB-INF新建include-nav.jsp 众筹平台 - 控制面板 ${sessionScope.loginAdmin.userName} 个人设置 消息 退出系统 帮助在webui\src\main\webapp\WEB-INF新建include-sidebar.jsp 控制面板 权限管理 3 用户维护 角色维护 菜单维护 业务审核 3 实名认证审核 广告审核 项目审核 业务管理 7 资质维护 分类管理 流程管理 广告管理 消息模板 项目分类 项目标签 参数管理修改webui\src\main\webapp\WEB-INF\admin-main.jsp > > > 控制面板目标为降低代码耦合,测试结果与前面相同 5.2 JSP模板创建IDEA创建模板路径与视频中略有区别: 点击“+”创建jsp文件即可,模板内容: > > > 6 登录状态检查 6.1 创建拦截器类在component\src\main\java\com\atguigu\crowd\mvc\interceptor新建LoginInterceptor.java package com.atguigu.crowd.mvc.interceptor; import com.atguigu.crowd.constant.CrowdConstant; import com.atguigu.crowd.entity.Admin; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class LoginInterceptor extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // 1.通过request对象获取Session对象 HttpSession session = request.getSession(); // 2.尝试从Session域获取Admin对象 Admin admin = (Admin) session.getAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN); // 3.判断Admin对象是否为空 if (admin == null) { // 4.抛出异常 throw new AccessForbidden(CrowdConstant.MESSAGE_ACCESS_FORBIDDEN); } // 5. 若admin不为空,则返回true放行 return true; } } 6.2 创建自定义异常在util\src\main\java\com\atguigu\crowd\exception新建AccessForbiddenException.java package com.atguigu.crowd.exception; /** * 表示用户没有登录就访问受保护资源时抛出的异常 */ public class AccessForbiddenException extends RuntimeException{ private static final long serialVersionUID = 1L; public AccessForbiddenException() { } public AccessForbiddenException(String message) { super(message); } public AccessForbiddenException(String message, Throwable cause) { super(message, cause); } public AccessForbiddenException(Throwable cause) { super(cause); } public AccessForbiddenException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } } 6.3 注册拦截器类修改webui\src\main\resources\spring-web-mvc.xml 6.4 基于xml的异常映射这部分没有相关视频,仅通过工程文档进行构建: 修改webui\src\main\resources\spring-web-mvc.xml system-error admin-login 6.5 基于注解的异常映射这部分没有相关视频,仅通过工程文档进行构建: 修改component\src\main\java\com\atguigu\crowd\mvc\config\CrowdExceptionResolver.java // 未登录异常 @ExceptionHandler(value = AccessForbiddenException.class) public ModelAndView resolveAccessForbiddenException(AccessForbiddenException exception, HttpServletRequest request, HttpServletResponse response) throws IOException { String viewName = "admin-login"; return commonResolveException(viewName,exception,request,response); } |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |